using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Module.UpDownLoad.Library;
using Framework.Library;
using Framework.Library.Components.Modules;
using Framework.Library.Components.Commands;
using Framework.Library.Utilities;

namespace Framework.Module.UpDownLoad.Admin
{
    public partial class files_edit : ModuleControlBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dtCommands = Categories_Controller.GetAll();
            LoadForParentItems(dtCommands);
            int itemid;
            if (this.Request.QueryString["id"] != null && Int32.TryParse(this.Request.QueryString["id"], out itemid))
            {
                if (!IsPostBack)
                {
                    this.LoadInfo(itemid);
                }
            }
        }

        private void LoadInfo(int itemid)
        {
            Files_Info info = Files_Controller.GetInfo(itemid);
            if (info != null)
            {
                txtDescription.Text = info.Description;
                lblName.Text = info.Name;
                lblSize.Text = Commas(info.Size) + " byte";
                lblFileType.Text = info.FileType;
                lblDateCreate.Text = ((DateTime)info.DateCreate).ToString();
                lblUploader.Text = info.Uploader;
                txtTitle.Text = info.Title;
                drgCategory.SelectedValue = info.CategoryID.ToString();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //Files_Info info = new Files_Info();
            Files_Info info = Files_Controller.GetInfo(Convert.ToInt32(Request.QueryString["id"]));
            if(info !=null)
            {
                try
                {
                    
                    
                    //info.CategoryID = Convert.ToInt32(drgCategory.SelectedValue);
                    info.Title = txtTitle.Text;
                    info.Description = txtDescription.Text;
                    //info.DateCreate = (DateTime)lblDateCreate.Text;
                    //info.Uploader = lblUploader.Text;
                    //info.FileType = lblFileType.Text;
                    //info.Name = lblName.Text;
                    Files_Controller.Update(info);
                    //try 
                    //{
                    //    //if (this.Request.QueryString["id"] != null && Int32.TryParse(this.Request.QueryString["id"], out itemid))
                    //    //{
                    //        //info.ID = itemid;
                    //    //}
                    //}
                    //catch (Exception ex)
                    //{
                    //    Response.Write(ex.Message);                        
                    //}

                    Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.FileManage)));
                }
                catch (Exception ex)
                {
                    lblStatus.Text = ex.Message;
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.FileManage)));
        }
        private void LoadForParentItems(DataTable dtCommands)
        {
            drgCategory.Items.Clear();
            Files_Controller.FillToListBox(drgCategory.Items, dtCommands, 0);
        }
        string Commas(long nNum)
        {
            string str = nNum.ToString();
            int nIndex = str.Length;
            while (nIndex > 3)
            {
                str = str.Insert(nIndex - 3, ",");
                nIndex -= 3;
            }
            return str;
        }
    }
}